﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WiimoteLib;
using System.Runtime.InteropServices;

namespace TayoukiNameSpace
{
    public partial class ShapeGame : TayoukiNameSpace.GameForm
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        private bool aBtnPressed;

        private bool bBtnPressed;

        private int maxX = 0;

        private int maxY = 0;

        private int minX = 0;

        private int minY = 0;


        public Image wii_IR;
        

        private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
        private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */

        public ShapeGame()
        {
            InitializeComponent();

            bBtnPressed = false;
            aBtnPressed = false;



        }


        public void testgame()
        {
        }


        private void ShapeGame_Load(object sender, EventArgs e)
        {
            this.textInstructions.Text = "Move the wiimote and press A to select a shape";

            changeAvatarImg(GameForm.GOOD_JOB);
            this.dragImages2.Visible = true;
            wii_IR = Properties.Resources.wii_IR;
            this.wiiInstructionsPictureBox.Image = wii_IR;
            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        }


        override public void processWiiMoteState(WiimoteState wms)
        {

            if (wms.ButtonState.B)
            {
                int x = wms.IRState.RawMidpoint.X;
                int y = wms.IRState.RawMidpoint.Y;

                //debugMaxValues(x, y);

                System.Windows.Forms.Cursor.Position = new System.Drawing.Point(GetXMousePoint(x), GetYMousePoint(y));

                bBtnPressed = true;
            }
            else
            {
                bBtnPressed = false;
            }

            if (wms.ButtonState.A)
            {
                if (!aBtnPressed)
                {
                    System.Console.WriteLine("A clicked");
                    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                }
                aBtnPressed = true;
            }
            else if (wms.ButtonState.Home)
            {
                gameClose();
            }

            else
            {
                aBtnPressed = false;
            }
        }

        private void debugMaxValues(int x, int y)
        {
            if (x > maxX)
            {
                maxX = x;
                System.Console.WriteLine("max X = " + maxX);
            }

            if (x < minX)
            {
                minX = x;
                System.Console.WriteLine("minX = " + minX);
            }

            if (y > maxY)
            {
                maxY = y;
                System.Console.WriteLine("maxY = " + maxY);
            }

            if (y < minY)
            {
                minY = y;
                System.Console.WriteLine("minY = " + minY);
            }

        }

        #region Mouse location based on resolution

        const int wiiMaxX = 873;
        const int wiiMaxY = 767;
        int GetXMousePoint(int wiiXValue)
        {
            //take the x value, which seems to be between
            int theReturn = 0;
            int scrnX = SystemInformation.PrimaryMonitorSize.Width;
            theReturn = ((scrnX * wiiXValue) / wiiMaxX);
            //theReturn = wiiXValue;
            theReturn = theReturn - 120;
            if (theReturn < 0)
            {
                theReturn = 0;
            }
            else if (theReturn > scrnX)
            {
                theReturn = scrnX;
            }
            theReturn = scrnX - theReturn;
            //Console.WriteLine("Mouse X = " + theReturn.ToString());
            return theReturn;
        }

        int GetYMousePoint(int wiiYValue)
        {
            int theReturn = 0;
            int scrnY = SystemInformation.PrimaryMonitorSize.Height;
            theReturn = ((scrnY * wiiYValue) / wiiMaxY);
            //theReturn = wiiYValue;
            //Console.WriteLine("Mouse Y = " + theReturn.ToString());
            return theReturn;
        }

        #endregion

        private void testBtn_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("You Clicked it!!");
        }

    }
}
